{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import pickle\n",
    "import gzip\n",
    "import random\n",
    "import numpy as np\n",
    "%matplotlib inline"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "def load_data():\n",
    "    f = gzip.open('../data/mnist.pkl.gz', 'rb')\n",
    "    #解压\n",
    "    training_data, validation_data, test_data = pickle.load(f, encoding='bytes')\n",
    "    #反序列化\n",
    "    f.close()\n",
    "    return (training_data, validation_data, test_data)\n",
    "'''\n",
    "1. pkl是python内置的一种格式，可以将python的各种数据结构序列化存储到磁盘中，需要时又可以读取并反序列化到内存中。\n",
    "2. mnist.pkl.gz做了两次操作，先pkl序列化，再gz压缩存储，所以要读取该文件，需要先解压再反序列化\n",
    "3. 该版本把原始的60000张训练集进一步划分成了50000张小训练集和10000张验证集\n",
    "'''\n",
    "\n",
    "\n",
    "\n",
    "def load_data_wrapper():\n",
    "    tr_d, va_d, te_d = load_data()\n",
    "    training_inputs = [np.reshape(x, (784, 1)) for x in tr_d[0]]\n",
    "    training_results = [vectorized_result(y) for y in tr_d[1]]\n",
    "    training_data = zip(training_inputs, training_results)\n",
    "    validation_inputs = [np.reshape(x, (784, 1)) for x in va_d[0]]\n",
    "    validation_data = zip(validation_inputs, va_d[1])\n",
    "    test_inputs = [np.reshape(x, (784, 1)) for x in te_d[0]]\n",
    "    test_data = zip(test_inputs, te_d[1])\n",
    "    return (training_data, validation_data, test_data)\n",
    "def vectorized_result(j):\n",
    "    e = np.zeros((10, 1))\n",
    "    e[j] = 1.0\n",
    "    return e\n",
    "'''\n",
    "将数据集序列化到文件中.需要注意pickle在序列化和反序列化时有不同的协议，可以用protocol参数进行设置。\n",
    "'''\n",
    "def write_data():\n",
    "    tr_d, va_d, te_d = load_data()\n",
    "    dataset=[tr_d,va_d,te_d]\n",
    "    f=gzip.open('/home/yihang/neural-networks-and-deep-learning/data/mnist.pkl.gz','wb')\n",
    "    pickle.dump(dataset,f,protocol=3)\n",
    "    f.close()\n",
    "'''\n",
    "将压缩的pkl格式存储的手写数字图片打印成png格式的图片\n",
    "第一个参数可以指定保存的路径以及文件名，会报错，delimiter=\"\\n\"表示分隔符\n",
    "cmap表示灰度图\n",
    "'''\n",
    "def plot_digit(X, y):\n",
    "    np.savetxt('/home/yihang/neural-networks-and-deep-learning/fig/%d.csv'%y, X, delimiter=',')  \n",
    "    import matplotlib.pyplot as plt\n",
    "    plt.imshow(X, cmap='Greys') # or 'Greys_r'\n",
    "    plt.savefig('/home/yihang/neural-networks-and-deep-learning/fig/%d.png'%y)\n",
    "    plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAPsAAAD4CAYAAAAq5pAIAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADh0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uMy4xLjAsIGh0dHA6Ly9tYXRwbG90bGliLm9yZy+17YcXAAANrUlEQVR4nO3df6xU9ZnH8c8jUo38MLhcDIqR2miiWa4UR0Lipqk2W8XEQP9oU2IaNjFAVCI1qKvdGPxT16WN0ZWEIpY1XUgjNRI1uzVIYhoSwkhY5UoWXGTby73hDrl/AFGDyrN/3MPmAne+c+85Z+ZMeN6vZDIz55kz58nA556Z8z0zX3N3Abj0XVZ1AwA6g7ADQRB2IAjCDgRB2IEgLu/kxmbOnOlz587t5CaBUI4ePaoTJ07YWLVCYTez+yS9JGmSpE3u/nzq8XPnzlW9Xi+ySQAJtVqtaS3323gzmyTpXyUtlnSbpGVmdlve5wPQXkU+sy+U9Jm7H3H3M5K2SVpSTlsAylYk7NdL+uuo+/3ZsvOY2Uozq5tZvdFoFNgcgCKKhH2sgwAXnXvr7hvdvebutZ6engKbA1BEkbD3S7ph1P05kgaKtQOgXYqEfa+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TMzOZLcklHJa1qS4cASjGeo/F/lmRjlN4rvx0A7cIZdEAQhB0IgrADQRB2IAjCDgRB2IEgCDsQBGEHgiDsQBCEHQiCsANBEHYgCMIOBEHYgSDM3Tu3MbOGpP8dtWimpBMda2BiurW3bu1Lore8yuztRncf8/ffOhr2izZuVnf3WmUNJHRrb93al0RveXWqN97GA0EQdiCIqsO+seLtp3Rrb93al0RveXWkt0o/swPonKr37AA6hLADQVQSdjO7z8z+28w+M7Onq+ihGTM7amafZNNQ1yvuZbOZDZnZgVHLrjGz983scHY95hx7FfXWFdN4J6YZr/S1q3r6845/ZjezSZIOSfp7Sf2S9kpa5u6fdrSRJszsqKSau1d+AoaZ/UDSaUn/5u5/my37Z0nD7v589odyhrv/Y5f09pyk01VP453NVjR79DTjkpZK+gdV+Nol+vqZOvC6VbFnXyjpM3c/4u5nJG2TtKSCPrqeu38oafiCxUskbclub9HIf5aOa9JbV3D3QXffl90+JencNOOVvnaJvjqiirBfL+mvo+73q7vme3dJfzKzj8xsZdXNjOFadx+URv7zSJpVcT8XajmNdyddMM1417x2eaY/L6qKsI81lVQ3jf/d5e4LJC2W9Gj2dhXjM65pvDtljGnGu0Le6c+LqiLs/ZJuGHV/jqSBCvoYk7sPZNdDkt5S901FffzcDLrZ9VDF/fy/bprGe6xpxtUFr12V059XEfa9km42s++a2Xck/VzSjgr6uIiZTckOnMjMpkj6sbpvKuodkpZnt5dLervCXs7TLdN4N5tmXBW/dpVPf+7uHb9Iul8jR+T/R9I/VdFDk75ukvRf2aWv6t4kbdXI27qvNfKO6CFJfyNpp6TD2fU1XdTbG5I+kfSxRoI1u6Le/k4jHw0/lrQ/u9xf9WuX6KsjrxunywJBcAYdEARhB4Ig7EAQhB0IgrADQRB2IAjCDgTxf79lHMIIiO5AAAAAAElFTkSuQmCC\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "#write_data()\n",
    "training_data, validation_data, test_data = load_data()\n",
    "'''\n",
    "training_data是一个tuple(元组),validation_data, test_data亦然\n",
    "training_data[0]是训练样本，是一个50000×784的矩阵，\n",
    "表示有50000个训练样本，每个训练样本是一个784的一维数组，784就是把一张28×28的图片展开reshape成的一维数组；\n",
    "training_data[1]是训练样本对应的类标号，大小为50000的一维数组，每个值为0~9中的某个数，表示对应样本的数字标号。\n",
    "'''\n",
    "X=np.reshape(validation_data[0][0], (28, 28))\n",
    "y=validation_data[1][0]\n",
    "plot_digit(X, y)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
